package org.hypirinha.utils.html2hypirinha;



import org.dom4j.Document;

public class BuilderClassGenerator {

    public String generate(Document document) {
    	StringBuilder output = new StringBuilder();

        output.append(imports());
        output.append(open());
        output.append(indent(new BuilderStatements(document).asStrings()));
        output.append(close());

        return output.toString();
    }

    private String indent(String[] statements) {
    	StringBuilder lines = new StringBuilder();
    	for (String statement : statements) {
			lines.append("        ").append(statement).append("\n");
		}
    	return lines.toString();
	}

	private String close() {
        return "        new PrintAdapter().print(html, System.out);\n" +
                "    }\n" +
                "}";
    }

    private String open() {
        return "\n" +
                "public class GeneratedGenerator {\n" +
                "\n" +
                "    public static void main(String[] args) {\n" +
                "        Html html = html();\n";
    }

    private String imports() {
        return "import static org.hypirinha.html.AttributeFactory.*;\n" +
                "import static org.hypirinha.html.ElementFactory.*;\n" +
                "import org.hypirinha.html.elements.*;\n" +
                "import org.hypirinha.html.output.PrintAdapter;\n";

    }

}